package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

//===================================================盘点主表=====================================================
type ErpStocks struct {
	Id           int64
	Number       string
	DepartmentId int64
	Products     *ErpProducts `orm:"rel(fk);null;on_delete(do_nothing)"`
	OperatorName string
	Auditing     int
	OperatorTime int64
	Typeid       int
}

//盘点主表查询项
type StocksQueryParam struct {
	BaseQueryParam
	Number    string `json:"number"`
	Depid     int64  `json:"depid"`
	StartDate string `json:"start_date"`
	EndDate   string `json:"end_date"`
}

func (a *ErpStocks) TableName() string {
	return StocksTableName()
}

func StocksTableName() string {
	return TableName("erp_stocks")
}

//查询条件
func StocksQuerySeter(params *StocksQueryParam) orm.QuerySeter {

	query := orm.NewOrm().QueryTable(StocksTableName())

	cond := orm.NewCondition()
	if params.Depid > 0 {
		cond = cond.And("department_id", params.Depid)
	}
	if len(params.Number) > 0 { //搜索直接查规格名

		cond = cond.And("Number__eq", params.Number)
	}

	if len(params.StartDate) > 0 {
		cond = cond.And("OperatorTime__gte", utils.GetDateParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		cond = cond.And("OperatorTime__lte", utils.GetDateParse(params.EndDate)+68400)
	}

	query = query.SetCond(cond)

	return query
}

func StocksPageList(params *StocksQueryParam) ([]*ErpStocks, int64) {

	ls := make([]*ErpStocks, 0)

	query := StocksQuerySeter(params)

	total, _ := query.Count()

	query.OrderBy("-Id").Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}

func queryStocksTimeSoltSeter(starttime, endtime int64) orm.QuerySeter {
	query := orm.NewOrm().QueryTable(StocksTableName())

	cond := orm.NewCondition()
	cond = cond.AndCond(cond.And("OperatorTime__gte", starttime).And("OperatorTime__lte", endtime))
	query = query.SetCond(cond)

	return query
}

//按时间段获取销售数据
func QueryStocksTimeSoltSeter(starttime, endtime int64) []*ErpStocks {
	data := make([]*ErpStocks, 0)
	queryStocksTimeSoltSeter(starttime, endtime).OrderBy("-id").All(&data)
	return data
}

//按时间段获取零售count
func QueryStocksTimeSoltSeterCount(starttime, endtime int64) int64 {

	count, _ := queryStocksTimeSoltSeter(starttime, endtime).Count()

	return count
}

func GetStocksById(id int64) (*ErpStocks, error) {

	r := new(ErpStocks)
	err := orm.NewOrm().QueryTable(StocksTableName()).RelatedSel("Products").Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}

	return r, nil
}

//===================================================盘点明细表==================================================
type StocksData struct {
	Sid              int64         `orm:"pk"`
	Stocks           *ErpStocks       `orm:"rel(fk)"`
	Inventory        *ErpInventory `orm:"rel(fk)"`
	PaperNumber      int64                            //帐面数量
	RealdiskNumber   int64                            //实盘数量
	ProfitlossNumber int64                            //盈亏数量
	IsChange         int                              //是否有变化1是0否
	LotNumber        string `json:"lot_number"`       //批号
	ManufactureDate  int64  `json:"manufacture_date"` //生产日期
	ExpiryDate       int64  `json:"expiry_date"`      //有效日期
}

//盘点主表查询项
type StocksDataQueryParam struct {
	BaseQueryParam
	StocksId   int64 `json:"stocks_id"`
	IsChange   int   `json:"is_change"`
	RelatedSel []interface{}
}

func (a *StocksData) TableName() string {
	return StocksDataTableName()
}

func StocksDataTableName() string {
	return TableName("erp_stocks_data")
}

//查询
func StocksDataQuerySeter(params *StocksDataQueryParam) orm.QuerySeter {
	query := orm.NewOrm().QueryTable(StocksDataTableName())
	return query
}

func StocksDataList(params *StocksDataQueryParam) []*StocksData {

	ls := make([]*StocksData, 0)

	query := orm.NewOrm().QueryTable(StocksDataTableName())
	if len(params.RelatedSel) > 0 {
		query = query.RelatedSel(params.RelatedSel...)
	}
	if params.StocksId > 0 {
		query = query.Filter("stocks_id", params.StocksId)
	}
	if params.IsChange > 0 {
		query = query.Filter("IsChange", 1)
	}

	query.OrderBy("sid").Limit(-1).All(&ls)
	return ls
}
